#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'ThreadLocal，一个存储着各个线程自己全局变量的副本'
import threading

'''
一个ThreadLocal变量虽然是全局变量，但每个线程都只能读写自己线程
的独立副本，互不干扰。	ThreadLocal解决了参数在一个线程中各个函
数之间互相传递的问题。
'''

# 创建全局ThreadLocal对象：
local_school = threading.local()


def process_student():
    # 获取当前线程关联的student:
    std = local_school.student
    print('Hello,%s(in %s)' % (std, threading.current_thread().name))


def process_thread(name):
    # 绑定ThreadLocal的student
    process_student()


if __name__ == '__main__':
    m = threading.Thread(target=process_thread, args=('MaoMao',))
    j = threading.Thread(target=process_thread, args=('JiJi',))
    m.start()
    j.start()
    m.join()
    j.join()
    # print(local_school.student) # AttributeError: '_thread._local' object has no attribute 'student'
